//
//  EarnActiveView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/10/16.
//

import UIKit

class EarnActiveView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 18)
        label.textColor = .black
        label.text = "活動任務"
        return label
    }()
    
    var taskClosure: AnyClosure<TaskModel>?
    
    var activeTaskArr = [TaskModel]() {
        didSet {
            isHidden = activeTaskArr.count == 0
            for item in subviews {
                if item.isKind(of: EarnActiveReadView.self) || item.isKind(of: EarnActiveRechargeView.self) {
                    item.removeFromSuperview()
                }
            }
            if activeTaskArr.count > 0 {
                for (i, v) in activeTaskArr.enumerated() {
                    if i == 0 {
                        let readTask = EarnActiveReadView()
                        addSubview(readTask)
                        readTask.snp.makeConstraints { make in
                            make.top.equalTo(42)
                            make.left.right.equalTo(0)
                            make.height.equalTo(77)
                        }
                        readTask.model = v
                        readTask.taskClosure = { [weak self] task in
                            self?.taskClosure?(task)
                        }
                    }else if i == 1 {
                        let rechargeTask = EarnActiveRechargeView()
                        addSubview(rechargeTask)
                        rechargeTask.snp.makeConstraints { make in
                            make.top.equalTo(42+77)
                            make.left.right.equalTo(0)
                            make.height.equalTo(170)
                        }
                        rechargeTask.model = v
                        rechargeTask.taskClosure = { [weak self] task in
                            self?.taskClosure?(task)
                        }
                    }
                }
            }
            layoutIfNeeded()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(12)
            make.height.equalTo(20)
        }
        wn_cornerWithBorder(8, 0.1, .white, 1, .init(white: 0, alpha: 0.1), .zero, 8, 1)
        layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class EarnActiveReadView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var taskDes: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_666666")
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView(image: UIImage(named: "reward_coin"))
    }()
    
    lazy var goldLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "red_FFB8C7")
        return label
    }()
    
    lazy var taskBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("閱讀", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        btn.wn_cornerWithBorder(16)
        btn.rx.tap.bind { [weak self] in
            self?.handleBtn()
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = .init(named: "red_FFB8C7")
        return btn
    }()
    
    var taskClosure: AnyClosure<TaskModel>?
    
    var model: TaskModel? {
        didSet {
            if let _ = model {
                taskName.text = "閱讀小說"
                taskDes.text = "享受閱讀，輕鬆取得金幣"
                goldLabel.text = "+?"
            }
        }
    }
    
    func handleBtn() {
        viewContainingController?.navigationController?.popToRootViewController(animated: true)
        wn_getTopVC()?.tabBarController?.tabBar.isHidden = false
        WNThread.wn_delay(0.1) {
            wn_getTopVC()?.tabBarController?.selectedIndex = 0
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        addSubview(taskDes)
        addSubview(goldImage)
        addSubview(goldLabel)
        addSubview(taskBtn)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(12)
            make.right.equalTo(-94)
        }
        taskDes.snp.makeConstraints { make in
            make.top.equalTo(taskName.snp.bottom).offset(6)
            make.left.equalTo(taskName.snp.left)
            make.right.equalTo(-94)
        }
        goldImage.snp.makeConstraints { (make) in
            make.top.equalTo(taskDes.snp.bottom).offset(6)
            make.left.equalTo(taskDes.snp.left)
            make.width.height.equalTo(16)
        }
        goldLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(goldImage.snp.centerY)
            make.left.equalTo(goldImage.snp.right).offset(6)
        }
        taskBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.size.equalTo(CGSize.init(width: 70, height: 32))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class EarnActiveRechargeView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var taskDes: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_666666")
        label.numberOfLines = 0
        return label
    }()
    
    lazy var slider: WNSlider = {
        var slider = WNSlider()
        slider.w = 0
        slider.minimumValue = 0
        slider.maximumValue = 1
        slider.setThumbImage(UIImage(), for: .normal)
        slider.minimumTrackTintColor = .init(named: "red_FE95B4")
        slider.maximumTrackTintColor = .init(named: "gray_ECE0E4")
        slider.backgroundColor = .clear
        slider.isEnabled = false
        return slider
    }()
    
    lazy var gift: EarnActiveRechargeItemView = {
        let view = EarnActiveRechargeItemView()
        return view
    }()
    
    lazy var gift1: EarnActiveRechargeItemView = {
        let view = EarnActiveRechargeItemView()
        view.taskClosure = { [weak self] m in
            self?.taskClosure?(m)
        }
        return view
    }()
    
    lazy var gift2: EarnActiveRechargeItemView = {
        let view = EarnActiveRechargeItemView()
        view.taskClosure = { [weak self] m in
            self?.taskClosure?(m)
        }
        return view
    }()
    
    lazy var gift3: EarnActiveRechargeItemView = {
        let view = EarnActiveRechargeItemView()
        view.taskClosure = { [weak self] m in
            self?.taskClosure?(m)
        }
        return view
    }()
    
    var taskClosure: AnyClosure<TaskModel>?

    var model: TaskModel? {
        didSet {
            if let m = model {
                taskName.text = "閱讀章節"
                taskDes.text = "特別優惠，特別充值"
                if let arr = m.children, arr.count > 2 {
                    gift.model = m
                    gift1.model = arr[0]
                    gift2.model = arr[1]
                    gift3.model = arr[2]
                    gift1.goldImage.image = UIImage(named: "coin_2")
                    gift2.goldImage.image = UIImage(named: "coin_4")
                    gift3.goldImage.image = UIImage(named: "coin_5")
                    let v = Float(m.chapter)/Float(arr[2].accrueNum)
                    slider.value = v > 1 ? 1 : v
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        addSubview(taskDes)
        addSubview(slider)
        addSubview(gift)
        addSubview(gift1)
        addSubview(gift2)
        addSubview(gift3)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(12)
        }
        taskDes.snp.makeConstraints { make in
            make.top.equalTo(taskName.snp.bottom).offset(6)
            make.left.equalTo(taskName.snp.left)
            make.right.equalTo(-94)
        }
        slider.snp.makeConstraints { make in
            make.bottom.equalTo(-23)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(2)
        }
        gift.snp.makeConstraints { make in
            make.left.equalTo(slider.snp.left).offset(0)
            make.bottom.equalTo(0)
            make.height.equalTo(102)
            make.width.equalTo(50)
        }
        gift1.snp.makeConstraints { make in
            make.left.equalTo(slider.snp.left).offset((ScreenW-60)*0.25-25)
            make.bottom.equalTo(0)
            make.size.equalTo(gift.snp.size)
        }
        gift2.snp.makeConstraints { (make) in
            make.left.equalTo(slider.snp.left).offset((ScreenW-60)*0.6-25)
            make.bottom.equalTo(0)
            make.size.equalTo(gift.snp.size)
        }
        gift3.snp.makeConstraints { (make) in
            make.right.equalTo(slider.snp.right).offset(3)
            make.bottom.equalTo(0)
            make.size.equalTo(gift.snp.size)
        }
        gift3.rechargeBtn.snp.updateConstraints { (make) in
            make.centerX.equalToSuperview().offset(10)
        }
        gift3.redView.snp.updateConstraints { (make) in
            make.centerX.equalTo(self.gift3.rechargeBtn.snp.centerX).offset(12)
        }
        layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class EarnActiveRechargeItemView: UIView {
    
    lazy var countLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "red_FFB8C7")
        label.textAlignment = .center
        return label
    }()

    lazy var rechargeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "read_coin_bg")?.withRenderingMode(.alwaysTemplate), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.taskClosure?(self?.model ?? TaskModel())
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(2)
        return btn
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView()
    }()
    
    lazy var goldLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    lazy var redView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "red_FE95B4")
        view.wn_cornerWithBorder(4)
        return view
    }()
    
    var taskClosure: AnyClosure<TaskModel>?

    var model: TaskModel? {
        didSet {
            if let m = model {
                rechargeBtn.isHidden = m.status == 0
                redView.isHidden = m.status == 0
                goldImage.isHidden = m.status == 0
                goldLabel.isHidden = m.status == 0
                goldLabel.text = "\(m.gold+m.old_gold)"
                countLabel.text = m.status == 0 ? "已讀\(m.chapter)章" : "\(m.accrueNum)章"
                let color = UIColor.init(named: m.status == 2 ? "red_FE95B4" : ( m.status == 1 ? "red_FFB8C7" : "gray_D0D0D0"))
                rechargeBtn.imageView?.tintColor = color
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(rechargeBtn)
        addSubview(goldImage)
        addSubview(goldLabel)
        addSubview(countLabel)
        addSubview(redView)
        rechargeBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.width.equalTo(50)
            make.height.equalTo(69)
        }
        goldImage.snp.makeConstraints { make in
            make.centerX.equalTo(rechargeBtn.snp.centerX)
            make.top.equalTo(rechargeBtn.snp.top).offset(6)
            make.width.height.equalTo(40)
        }
        goldLabel.snp.makeConstraints { make in
            make.centerX.equalTo(rechargeBtn.snp.centerX)
            make.bottom.equalTo(rechargeBtn.snp.bottom).offset(-3)
        }
        countLabel.snp.makeConstraints { make in
            make.centerX.equalTo(rechargeBtn.snp.centerX)
            make.bottom.equalTo(0)
        }
        redView.snp.makeConstraints { (make) in
            make.centerX.equalTo(rechargeBtn.snp.centerX)
            make.bottom.equalTo(-18)
            make.width.height.equalTo(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
